//
//  LanguageViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/10/8.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "LanguageViewController.h"
#import "LanguageTableViewCell.h"
@interface LanguageViewController ()<QMUITableViewDataSource,QMUITableViewDelegate>
@property(nonatomic, strong) NSArray *dataSource;
@end

@implementation LanguageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSource = @[@"中文简体",@"中文繁體"];
    self.tableView.frame = self.view.bounds;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    LanguageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[LanguageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSString *lname = self.dataSource[indexPath.row];
    cell.textLabel.text = lname;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray  *languages = [NSLocale preferredLanguages];
    NSString *lname = self.dataSource[indexPath.row];
    if([lname isEqualToString:@"中文简体"])
    {
        [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:KAppLanguage];
        
        // 强制 成 简体中文
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-hans",nil] forKey:@"AppleLanguages"];
   
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-hant",nil] forKey:@"AppleLanguages"];
        
        [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:KAppLanguage];
    }
    
    [HUDHelper alert:@"我们将关闭启动程序，请重新启动" action:^{
        abort();
    }];
    
    [self.tableView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
